﻿using ECCore;
using Configuration.ServerApi.Entity;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Configuration.ServerApi.Helper
{
    /// <summary>
    /// 电化学测试仪器通讯工具类
    /// </summary>
    public class ElecMachinesHelper
    {
        #region 公共变量

        /// <summary>
        /// 电化学测试仪器通讯组件
        /// </summary>
        ElecMachines ElecMachines = new ElecMachines();

        // 实验运行状态标识符
        const int NoneDataType = 0;
        const int Voltammetry = 1;
        const int Impedance = 2;
        const int Energ = 3;
        const int StaticEIS = 4;
        int LastResultDataType = NoneDataType;

        /// <summary>
        /// 运行实验日志记录数据
        /// </summary>
        public ObservableCollection<string> LogData = new ObservableCollection<string>();

        /// <summary>
        /// 运行日志数据
        /// </summary>
        public class RunRecordData
        {
            /// <summary>
            /// Capacity(mAh)
            /// </summary>
            public string Capacity { get; set; } = "--";

            /// <summary>
            /// Current
            /// </summary>
            public string Current { get; set; } = "--";

            /// <summary>
            /// EDC(mV)
            /// </summary>
            public string E { get; set; } = "--";

            /// <summary>
            /// EDC(mV)
            /// </summary>
            public string EDC { get; set; } = "--";

            /// <summary>
            /// Energy(Wh)
            /// </summary>
            public string Energy { get; set; } = "--";

            /// <summary>
            /// Freq(Hz)
            /// </summary>
            public string Freq { get; set; } = "--";

            /// <summary>
            /// I(mA)
            /// </summary>
            public string I { get; set; } = "--";

            /// <summary>
            /// JiaoDu
            /// </summary>
            public string JiaoDu { get; set; } = "--";

            /// <summary>
            /// LstTime
            /// </summary>
            public string LstTime { get; set; } = "--";

            /// <summary>
            /// P(W)
            /// </summary>
            public string P { get; set; } = "--";

            /// <summary>
            /// Phase(°)
            /// </summary>
            public string Phase { get; set; } = "--";

            /// <summary>
            /// Q(mC)
            /// </summary>
            public string Q { get; set; } = "--";

            /// <summary>
            /// Time(s)
            /// </summary>
            public string Time { get; set; } = "--";

            /// <summary>
            /// Voltage
            /// </summary>
            public string Voltage { get; set; } = "--";

            /// <summary>
            /// Z(Ω)
            /// </summary>
            public string Z { get; set; } = "--";

            /// <summary>
            /// Zre(Ω)
            /// </summary>
            public string Zre { get; set; } = "--";

            /// <summary>
            /// Zim(Ω)
            /// </summary>
            public string Zim { get; set; } = "--";

            /// <summary>
            /// ZValue
            /// </summary>
            public string ZValue { get; set; } = "--";

        }

        #endregion

        #region 初始化通讯连接

        /// <summary>
        /// 初始化通讯连接
        /// </summary>
        /// <returns>成功或失败，返回结果信息</returns>
        public (bool, string) InitConnect()
        {
            try
            {
                bool isSuccess = ElecMachines.Init($"{AppDomain.CurrentDomain.BaseDirectory}DHInterface");
                if (isSuccess)
                {
                    // 挂载异常处理事件
                    ElecMachines.ExperimentExceptionEvent -= ElecMachines_ExperimentExceptionEvent;
                    ElecMachines.ExperimentExceptionEvent += ElecMachines_ExperimentExceptionEvent;

                    return (true, $"查找仪器成功");
                }
                else
                {
                    return (true, $"未查找到可用仪器!!");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return (false, $"查找仪器时发生异常, 错误信息: {ex.Message}");
            }
        }

        /// <summary>
        /// 仪器异常事件
        /// 可以看下这里的代码，需要注册事件
        /// </summary>
        /// <param name="exceptionId"></param>
        /// <param name="strInfo"></param>
        void ElecMachines_ExperimentExceptionEvent(int exceptionId, string strInfo)
        {
            // 这里代表实验异常了，内部已经自动停止实验了，这里不需要再停止
            switch (exceptionId)
            {
                case MachineExceptionId.MachineDisconnect:
                    {

                    }
                    break;
            }
        }

        #endregion

        #region 获取可用设备列表

        /// <summary>
        /// 获取可用设备列表
        /// </summary>
        /// <returns></returns>
        public Dictionary<int, string> GetMachineData()
        {
            try
            {
                int[] MachineIds = ElecMachines.GetMachineId();
                Dictionary<int, string> MachineData = new Dictionary<int, string>();
                foreach (int id in MachineIds)
                {
                    MachineData.Add(id, $"{id}号机");
                }
                return MachineData;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return new Dictionary<int, string>();
            }
        }

        #endregion

        #region 获取可用实验列表

        /// <summary>
        /// 获取可用实验列表
        /// </summary>
        /// <returns></returns>
        public List<string> GetExperimentList()
        {
            List<string> ExperimentName = new List<string>
            {
                "开路电位(电分析)","线性扫描", "循环伏安法","循环伏安法(多循环)",
                "恒电位","恒电流",
                "交流阻抗(电压)", "交流阻抗(电流)",
                "阶梯循环伏安法","阶梯循环伏安法(多循环)",
                "阶梯线性扫描伏安法(SLSV)",
                "Tafel"
            };
            return ExperimentName;
        }

        #endregion

        #region 开始实验

        /// <summary>
        /// 开始实验
        /// </summary>
        /// <param name="ExpType">实验类型</param>
        /// <returns>成功或失败，返回结果信息</returns>
        public (bool, string) StartExp(int ExpType)
        {
            try
            {
                // 根据界面选择判断实验类型
                ExperimentType experimentType = ExperimentType.Non;
                switch (ExpType)
                {
                    case 0:
                        experimentType = ExperimentType.Open_Circuit_Voltammetry;
                        break;
                    case 1:
                        experimentType = ExperimentType.Linear_Scan_Voltammetry;
                        break;
                    case 2:
                        experimentType = ExperimentType.Circle_Voltammetry_Single;
                        break;
                    case 3:
                        experimentType = ExperimentType.CircleVoltammetryMultipleCycles;
                        break;
                    case 4:
                        experimentType = ExperimentType.Chronoamperonetry;
                        break;
                    case 5:
                        experimentType = ExperimentType.Chronopotentiometry;
                        break;
                    case 6:
                        experimentType = ExperimentType.Potentiostatic_Eis;
                        break;
                    case 7:
                        experimentType = ExperimentType.Galvanostatic_Eis;
                        break;
                    case 8:
                        experimentType = ExperimentType.Staircase_Cyclic_Voltammetry_Single;
                        break;
                    case 9:
                        experimentType = ExperimentType.Staircase_Cyclic_Voltammetry_MultipleCycles;
                        break;
                    case 10:
                        experimentType = ExperimentType.Staircase_Linear_Scan_Voltammetry;
                        break;
                    case 11:
                        experimentType = ExperimentType.Tafel;
                        break;
                }

                // 获取电压，电流量程
                string[] VolRand = ElecMachines.GetVolRand(experimentType);
                string[] CurrentRand = ElecMachines.GetCurrentRand(experimentType);
                System.Diagnostics.Trace.WriteLine(VolRand.Length);
                System.Diagnostics.Trace.WriteLine(CurrentRand.Length);

                // 错误消息内容
                string strErrorMessage = "";

                // 根据实验类型创建对应的实验参数
                switch (experimentType)
                {
                    case ExperimentType.Open_Circuit_Voltammetry:
                        {
                            float timePerPoint = 0.1f;
                            float continueTime = 10;

                            strErrorMessage = ElecMachines.Start_OpenCircuit(timePerPoint, continueTime);
                        }
                        break;
                    case ExperimentType.Linear_Scan_Voltammetry:
                        {
                            // (1) 开始电位
                            float InitialPotential = 0;
                            // (2) 开始电位的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            int InitialPotentialVSType = (int)VSType.VSRefType;
                            // (3) 最终电位
                            float FinallyPotential = 1;
                            // (4) 最终电位的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            int FinallyPotentialVSType = (int)VSType.VSRefType;
                            // (5) 扫描速率
                            float ScanRate = 0.1f;
                            // (6) 这里电压量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            int isVoltageRandAuto = 1;
                            string VoltageRand = "1000";
                            // (7) 这里电流量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            int isCurrentRandAuto = 1;
                            string currentRand = "1000";

                            strErrorMessage = ElecMachines.Start_Linear_Scan_Voltammetry(InitialPotential, InitialPotentialVSType, FinallyPotential, FinallyPotentialVSType, ScanRate, isVoltageRandAuto, VoltageRand, isCurrentRandAuto, currentRand);
                        }
                        break;
                    case ExperimentType.Circle_Voltammetry_Single:
                        {
                            // (1) 开始电位
                            float InitialPotential = -1;
                            // (2) 开始电位的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            int InitialPotentialVSType = (int)VSType.VSRefType;
                            // (3) 顶点电位
                            float TopPotential = 1;
                            // (4) 顶点电位的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            int TopPotentialVSType = (int)VSType.VSRefType;
                            // (5) 最终电位
                            float FinallyPotential = -1;
                            // (6) 最终电位的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            int FinallyPotentialVSType = (int)VSType.VSRefType;
                            // (7) 扫描速率
                            float ScanRate = 0.1f;
                            // (8) 这里电压量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            int isVoltageRandAuto = 1;
                            string VoltageRand = "1000";
                            // (9) 这里电流量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            int isCurrentRandAuto = 1;
                            string currentRand = "1000";

                            strErrorMessage = ElecMachines.Start_Circle_Voltammetry_Single(InitialPotential, InitialPotentialVSType, TopPotential, TopPotentialVSType, FinallyPotential, FinallyPotentialVSType, ScanRate, isVoltageRandAuto, VoltageRand, isCurrentRandAuto, currentRand);
                        }
                        break;
                    case ExperimentType.CircleVoltammetryMultipleCycles:
                        {
                            // (1) 是否使用初始电位
                            bool IsUseInitialPotential = false;
                            // (2) 开始电位
                            float InitialPotential = -0.2f;
                            // (3) 开始电位的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            int InitialPotentialVSType = (int)VSType.VSRefType;
                            // (4) 顶点电位1
                            float TopPotential1 = 0.8f;
                            // (5) 顶点电位1的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            int TopPotential1VSType = (int)VSType.VSRefType;
                            // (6) 顶点电位2
                            float TopPotential2 = -0.2f;
                            // (7) 顶点电位2的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            int TopPotential2VSType = (int)VSType.VSRefType;
                            // (8) 是否使用最终电位
                            bool IsUseFinallyPotential = false;
                            // (9) 最终电位
                            float FinallyPotential = -0.2f;
                            // (10) 最终电位的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            int FinallyPotentialVSType = (int)VSType.VSRefType;
                            // (11) 扫描速率
                            float ScanRate = 0.1f;
                            // (12) 扫描圈数
                            int CycleCount = 2;
                            // (13) 这里电压量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            int isVoltageRandAuto = 1;
                            string VoltageRand = "1000";
                            // (14) 这里电流量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            int isCurrentRandAuto = 1;
                            string currentRand = "1000";

                            strErrorMessage = ElecMachines.Start_Circle_Voltammetry_Multi(
                                IsUseInitialPotential, InitialPotential, InitialPotentialVSType,
                                TopPotential1, TopPotential1VSType, TopPotential2, TopPotential2VSType,
                                IsUseFinallyPotential, FinallyPotential, FinallyPotentialVSType,
                                ScanRate, CycleCount, isVoltageRandAuto, VoltageRand, isCurrentRandAuto, currentRand);
                        }
                        break;
                    case ExperimentType.Staircase_Linear_Scan_Voltammetry:
                        {
                            // (1) 开始电位
                            float InitialPotential = 0;
                            // (2) 开始电位的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            int InitialPotentialVSType = (int)VSType.VSRefType;
                            // (3) 最终电位
                            float FinallyPotential = 1;
                            // (4) 最终电位的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            int FinallyPotentialVSType = (int)VSType.VSRefType;
                            // (5) 阶跃高度
                            float StepHeight = 10;
                            // (6) 阶跃时间
                            float StepTime = 0.1f;
                            // (7) 这里电压量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            int isVoltageRandAuto = 1;
                            string VoltageRand = "1000";
                            // (8) 这里电流量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            int isCurrentRandAuto = 1;
                            string currentRand = "1000";

                            strErrorMessage = ElecMachines.Start_Staircase_Linear_Scan(InitialPotential, InitialPotentialVSType, FinallyPotential, FinallyPotentialVSType, StepHeight, StepTime, isVoltageRandAuto, VoltageRand, isCurrentRandAuto, currentRand);
                        }
                        break;
                    case ExperimentType.Staircase_Cyclic_Voltammetry_Single:
                        {
                            // (1) 开始电位
                            float InitialPotential = -1;
                            // (2) 开始电位的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            int InitialPotentialVSType = (int)VSType.VSOCType;
                            // (3) 顶点电位
                            float TopPotential = 1;
                            // (4) 顶点电位的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            int TopPotentialVSType = (int)VSType.VSOCType;
                            // (5) 最终电位
                            float FinallyPotential = -1;
                            // (6) 最终电位的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            int FinallyPotentialVSType = (int)VSType.VSOCType;
                            // (7) 阶跃高度
                            float StepHeight = 10;
                            // (8) 阶跃时间
                            float StepTime = 1f;
                            // (9) 这里电压量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            int isVoltageRandAuto = 1;
                            string VoltageRand = "1000";
                            // (10) 这里电流量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            int isCurrentRandAuto = 1;
                            string currentRand = "1000";

                            strErrorMessage = ElecMachines.Start_Staircase_Cyclic_Voltammetry_Single(InitialPotential, InitialPotentialVSType, TopPotential, TopPotentialVSType, FinallyPotential, FinallyPotentialVSType, StepHeight, StepTime, isVoltageRandAuto, VoltageRand, isCurrentRandAuto, currentRand);
                        }
                        break;
                    case ExperimentType.Staircase_Cyclic_Voltammetry_MultipleCycles:
                        {
                            // (1) 开始电位
                            float InitialPotential = -0.2f;
                            // (2) 开始电位的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            int InitialPotentialVSType = (int)VSType.VSRefType;
                            // (3) 顶点电位1
                            float TopPotential1 = 0.8f;
                            // (4) 顶点电位1的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            int TopPotential1VSType = (int)VSType.VSRefType;
                            // (5) 顶点电位2
                            float TopPotential2 = -0.2f;
                            // (6) 顶点电位2的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            int TopPotential2VSType = (int)VSType.VSRefType;
                            // (7) 最终电位
                            float FinallyPotential = -0.2f;
                            // (8) 最终电位的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            int FinallyPotentialVSType = (int)VSType.VSRefType;
                            //( 9) 阶跃高度
                            float StepHeight = 10;
                            // (10) 阶跃时间
                            float StepTime = 1f;
                            // (11) 扫描速率
                            int CycleCount = 2;
                            // (12) 这里电压量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            int isVoltageRandAuto = 1;
                            string VoltageRand = "1000";
                            // (13) 这里电流量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            int isCurrentRandAuto = 1;
                            string currentRand = "1000";

                            strErrorMessage = ElecMachines.Start_Staircase_Cyclic_Voltammetry_MultipleCycles(InitialPotential, InitialPotentialVSType, TopPotential1, TopPotential1VSType, TopPotential2, TopPotential2VSType, FinallyPotential, FinallyPotentialVSType, StepHeight, StepTime, CycleCount, isVoltageRandAuto, VoltageRand, isCurrentRandAuto, currentRand);
                        }
                        break;
                    case ExperimentType.Chronoamperonetry:
                        {
                            // (1) 每点时间
                            float timePerPoint = 0.1f;
                            // (2) 持续时间
                            float continueTime = 10;
                            // (3) 阶跃电位
                            float Potential = 0.1f;
                            // (4) 阶跃电位的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            int PotentialVSType = (int)VSType.VSOCType;
                            // (5) 这里电压量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            int isVoltageRandAuto = 1;
                            string VoltageRand = "1000";
                            // (6) 这里电流量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            int isCurrentRandAuto = 1;
                            string currentRand = "1000";

                            strErrorMessage = ElecMachines.Start_ChronoamperonetryParam(timePerPoint, continueTime, Potential, PotentialVSType, isVoltageRandAuto, VoltageRand, isCurrentRandAuto, currentRand);
                        }
                        break;
                    case ExperimentType.Chronopotentiometry:
                        {
                            // (1) 每点时间
                            float timePerPoint = 0.1f;
                            // (2) 持续时间
                            float continueTime = 10;
                            // (3) 电流
                            float current = 0.1f;
                            // (4) 这里电压没有自动量程
                            string VoltageRand = "10000";
                            // (5) 这里电流量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            int isCurrentRandAuto = 1;
                            string currentRand = "1000";

                            strErrorMessage = ElecMachines.Start_ChronopotentiometryParam(timePerPoint, continueTime, current, VoltageRand, isCurrentRandAuto, currentRand);
                        }
                        break;
                    case ExperimentType.Potentiostatic_Eis:
                        {
                            // 开始频率
                            double StartFreq = 10000;
                            // 结束频率
                            double EndFreq = 0.1;
                            // 振幅
                            float Amplitude = 0.01f;
                            // 间隔类型，这里设置为对数，也可以设置为线性，如下面注释
                            //int intervalType =(int)IntervalType.Line
                            int intervalType = (int)IntervalType.Log;
                            // 点数
                            int PointCount = 10;
                            // 电位
                            float Voltage = 0;
                            // 电位相对类型
                            int VoltageVSType = (int)VSType.VSOCType;
                            // 这里量程的设置同上面的线性扫描
                            int isVoltageRandAuto = 1;
                            string VoltageRand = "1000";

                            int isCurrentRandAuto = 1;
                            string currentRand = "1000";

                            strErrorMessage = ElecMachines.Start_EIS(true, StartFreq, EndFreq, Amplitude, intervalType, PointCount, Voltage, VoltageVSType, isVoltageRandAuto, VoltageRand, isCurrentRandAuto, currentRand);
                        }
                        break;
                    case ExperimentType.Galvanostatic_Eis:
                        {
                            // 开始频率
                            double StartFreq = 10000;
                            // 结束频率
                            double EndFreq = 0.1;
                            // 振幅----------这里如果是7002B,可以把默认值设置为100
                            float Amplitude = 1;
                            // 间隔类型，这里设置为对数，也可以设置为线性，如下面注释
                            //int intervalType =(int)IntervalType.Line
                            int intervalType = (int)IntervalType.Log;
                            // 点数
                            int PointCount = 10;
                            // 直接输出
                            float DirectCurrent = 0;
                            // 直接输出相对类型
                            int DirectCurrentVSType = (int)VSType.VSRefType;
                            // 这里量程的设置同上面的线性扫描
                            int isVoltageRandAuto = 1;
                            string VoltageRand = "1000";

                            int isCurrentRandAuto = 1;
                            string currentRand = "1000";

                            strErrorMessage = ElecMachines.Start_EIS(false, StartFreq, EndFreq, Amplitude, intervalType, PointCount, DirectCurrent, DirectCurrentVSType, isVoltageRandAuto, VoltageRand, isCurrentRandAuto, currentRand);
                        }
                        break;
                    case ExperimentType.Tafel:
                        {
                            // (1) 开始电位
                            float InitialPotential = -0.25f;
                            // (2) 开始电位的参考类型，这里为VSOCType,也可以设置为VSTypeRef.
                            int InitialPotentialVSType = (int)VSType.VSOCType;
                            // (3) 最终电位
                            float FinallyPotential = 0.25f;
                            // (4) 最终电位的参考类型，这里为VSOCType,也可以设置为VSType.VSTypeRef
                            int FinallyPotentialVSType = (int)VSType.VSOCType;
                            // (5) 阶跃高度
                            float StepHeight = 0.5f;
                            // (6) 阶跃时间
                            float StepTime = 3;
                            // (7) 这里电压量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            int isVoltageRandAuto = 1;
                            string VoltageRand = "1000";
                            // (8) 这里电流量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            int isCurrentRandAuto = 1;
                            string currentRand = "1000";

                            strErrorMessage = ElecMachines.Start_Tafel(InitialPotential, InitialPotentialVSType, FinallyPotential, FinallyPotentialVSType, StepHeight, StepTime, isVoltageRandAuto, VoltageRand, isCurrentRandAuto, currentRand);
                        }
                        break;
                }

                // 查看是否有错误信息，如果没有就是成功
                if (string.IsNullOrEmpty(strErrorMessage))
                {
                    if (ElecMachines.IsExperimenting())
                    {
                        // 这里成功了
                        return (true, "实验初始化成功, 正在执行");
                    }
                }

                // 这里失败了，可以查看失败原因
                return (false, strErrorMessage);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return (false, ex.Message);
            }
        }

        /// <summary>
        /// 开始实验
        /// </summary>
        /// <param name="ExpType">实验类型</param>
        /// <param name="ConfigData">实验配置信息</param>
        /// <returns>成功或失败，返回结果信息</returns>
        public (bool, string) StartExp(int ExpType, bis_expconfig_electrochemistrytesting ConfigData)
        {
            try
            {
                // 根据界面选择判断实验类型
                ExperimentType experimentType = ExperimentType.Non;
                switch (ExpType)
                {
                    case 0:
                        experimentType = ExperimentType.Open_Circuit_Voltammetry;
                        break;
                    case 1:
                        experimentType = ExperimentType.Linear_Scan_Voltammetry;
                        break;
                    case 2:
                        experimentType = ExperimentType.Circle_Voltammetry_Single;
                        break;
                    case 3:
                        experimentType = ExperimentType.CircleVoltammetryMultipleCycles;
                        break;
                    case 4:
                        experimentType = ExperimentType.Chronoamperonetry;
                        break;
                    case 5:
                        experimentType = ExperimentType.Chronopotentiometry;
                        break;
                    case 6:
                        experimentType = ExperimentType.Potentiostatic_Eis;
                        break;
                    case 7:
                        experimentType = ExperimentType.Galvanostatic_Eis;
                        break;
                    case 8:
                        experimentType = ExperimentType.Staircase_Cyclic_Voltammetry_Single;
                        break;
                    case 9:
                        experimentType = ExperimentType.Staircase_Cyclic_Voltammetry_MultipleCycles;
                        break;
                    case 10:
                        experimentType = ExperimentType.Staircase_Linear_Scan_Voltammetry;
                        break;
                    case 11:
                        experimentType = ExperimentType.Tafel;
                        break;
                }

                // 获取电压，电流量程
                string[] VolRand = ElecMachines.GetVolRand(experimentType);
                string[] CurrentRand = ElecMachines.GetCurrentRand(experimentType);
                System.Diagnostics.Trace.WriteLine(VolRand.Length);
                System.Diagnostics.Trace.WriteLine(CurrentRand.Length);

                // 错误消息内容
                string strErrorMessage = "";

                // 根据实验类型创建对应的实验参数
                switch (experimentType)
                {
                    case ExperimentType.Open_Circuit_Voltammetry:
                        {
                            //float timePerPoint = 0.1f;
                            //float continueTime = 10;

                            strErrorMessage = ElecMachines.Start_OpenCircuit(
                                (float)ConfigData.TimePerPoint,
                                (float)ConfigData.ContinueTime
                            );
                        }
                        break;
                    case ExperimentType.Linear_Scan_Voltammetry:
                        {
                            //// (1) 开始电位
                            //float InitialPotential = 0;
                            //// (2) 开始电位的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            //int InitialPotentialVSType = (int)VSType.VSRefType;
                            //// (3) 最终电位
                            //float FinallyPotential = 1;
                            //// (4) 最终电位的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            //int FinallyPotentialVSType = (int)VSType.VSRefType;
                            //// (5) 扫描速率
                            //float ScanRate = 0.1f;
                            //// (6) 这里电压量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            //int isVoltageRandAuto = 1;
                            //string VoltageRand = "1000";
                            //// (7) 这里电流量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            //int isCurrentRandAuto = 1;
                            //string currentRand = "1000";

                            strErrorMessage = ElecMachines.Start_Linear_Scan_Voltammetry(
                                (float)ConfigData.InitialPotential,
                                ConfigData.InitialPotentialVSType.Value,
                                (float)ConfigData.FinallyPotential,
                                ConfigData.FinallyPotentialVSType.Value,
                                (float)ConfigData.ScanRate,
                                ConfigData.IsVoltageRandAuto.Value,
                                $"{(int)ConfigData.VoltageRand}",
                                ConfigData.IsCurrentRandAuto.Value,
                                $"{(int)ConfigData.CurrentRand}"
                            );
                        }
                        break;
                    case ExperimentType.Circle_Voltammetry_Single:
                        {
                            //// (1) 开始电位
                            //float InitialPotential = -1;
                            //// (2) 开始电位的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            //int InitialPotentialVSType = (int)VSType.VSRefType;
                            //// (3) 顶点电位
                            //float TopPotential = 1;
                            //// (4) 顶点电位的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            //int TopPotentialVSType = (int)VSType.VSRefType;
                            //// (5) 最终电位
                            //float FinallyPotential = -1;
                            //// (6) 最终电位的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            //int FinallyPotentialVSType = (int)VSType.VSRefType;
                            //// (7) 扫描速率
                            //float ScanRate = 0.1f;
                            //// (8) 这里电压量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            //int isVoltageRandAuto = 1;
                            //string VoltageRand = "1000";
                            //// (9) 这里电流量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            //int isCurrentRandAuto = 1;
                            //string currentRand = "1000";

                            strErrorMessage = ElecMachines.Start_Circle_Voltammetry_Single(
                                (float)ConfigData.InitialPotential,
                                ConfigData.InitialPotentialVSType.Value,
                                (float)ConfigData.TopPotential1,
                                ConfigData.TopPotential1VSType.Value,
                                (float)ConfigData.FinallyPotential,
                                ConfigData.FinallyPotentialVSType.Value,
                                (float)ConfigData.ScanRate,
                                ConfigData.IsVoltageRandAuto.Value,
                                $"{(int)ConfigData.VoltageRand}",
                                ConfigData.IsCurrentRandAuto.Value,
                                $"{(int)ConfigData.CurrentRand}"
                            );
                        }
                        break;
                    case ExperimentType.CircleVoltammetryMultipleCycles:
                        {
                            //// (1) 是否使用初始电位
                            //bool IsUseInitialPotential = false;
                            //// (2) 开始电位
                            //float InitialPotential = -0.2f;
                            //// (3) 开始电位的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            //int InitialPotentialVSType = (int)VSType.VSRefType;
                            //// (4) 顶点电位1
                            //float TopPotential1 = 0.8f;
                            //// (5) 顶点电位1的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            //int TopPotential1VSType = (int)VSType.VSRefType;
                            //// (6) 顶点电位2
                            //float TopPotential2 = -0.2f;
                            //// (7) 顶点电位2的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            //int TopPotential2VSType = (int)VSType.VSRefType;
                            //// (8) 是否使用最终电位
                            //bool IsUseFinallyPotential = false;
                            //// (9) 最终电位
                            //float FinallyPotential = -0.2f;
                            //// (10) 最终电位的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            //int FinallyPotentialVSType = (int)VSType.VSRefType;
                            //// (11) 扫描速率
                            //float ScanRate = 0.1f;
                            //// (12) 扫描圈数
                            //int CycleCount = 2;
                            //// (13) 这里电压量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            //int isVoltageRandAuto = 1;
                            //string VoltageRand = "1000";
                            //// (14) 这里电流量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            //int isCurrentRandAuto = 1;
                            //string currentRand = "1000";

                            strErrorMessage = ElecMachines.Start_Circle_Voltammetry_Multi(
                                ConfigData.IsUseInitialPotential.Value == 1,
                                (float)ConfigData.InitialPotential,
                                ConfigData.InitialPotentialVSType.Value,
                                (float)ConfigData.TopPotential1,
                                ConfigData.TopPotential1VSType.Value,
                                (float)ConfigData.TopPotential2,
                                ConfigData.TopPotential2VSType.Value,
                                ConfigData.IsUseFinallyPotential.Value == 1,
                                (float)ConfigData.FinallyPotential,
                                ConfigData.FinallyPotentialVSType.Value,
                                (float)ConfigData.ScanRate,
                                (int)ConfigData.CycleCount,
                                ConfigData.IsVoltageRandAuto.Value,
                                $"{(int)ConfigData.VoltageRand}",
                                ConfigData.IsCurrentRandAuto.Value,
                                $"{(int)ConfigData.CurrentRand}"
                            );
                        }
                        break;
                    case ExperimentType.Staircase_Linear_Scan_Voltammetry:
                        {
                            //// (1) 开始电位
                            //float InitialPotential = 0;
                            //// (2) 开始电位的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            //int InitialPotentialVSType = (int)VSType.VSRefType;
                            //// (3) 最终电位
                            //float FinallyPotential = 1;
                            //// (4) 最终电位的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            //int FinallyPotentialVSType = (int)VSType.VSRefType;
                            //// (5) 阶跃高度
                            //float StepHeight = 10;
                            //// (6) 阶跃时间
                            //float StepTime = 0.1f;
                            //// (7) 这里电压量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            //int isVoltageRandAuto = 1;
                            //string VoltageRand = "1000";
                            //// (8) 这里电流量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            //int isCurrentRandAuto = 1;
                            //string currentRand = "1000";

                            strErrorMessage = ElecMachines.Start_Staircase_Linear_Scan(
                                (float)ConfigData.InitialPotential,
                                ConfigData.InitialPotentialVSType.Value,
                                (float)ConfigData.FinallyPotential,
                                ConfigData.FinallyPotentialVSType.Value,
                                (float)ConfigData.StepHeight,
                                (float)ConfigData.StepTime,
                                ConfigData.IsVoltageRandAuto.Value,
                                $"{(int)ConfigData.VoltageRand}",
                                ConfigData.IsCurrentRandAuto.Value,
                                $"{(int)ConfigData.CurrentRand}"
                            );
                        }
                        break;
                    case ExperimentType.Staircase_Cyclic_Voltammetry_Single:
                        {
                            //// (1) 开始电位
                            //float InitialPotential = -1;
                            //// (2) 开始电位的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            //int InitialPotentialVSType = (int)VSType.VSOCType;
                            //// (3) 顶点电位
                            //float TopPotential = 1;
                            //// (4) 顶点电位的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            //int TopPotentialVSType = (int)VSType.VSOCType;
                            //// (5) 最终电位
                            //float FinallyPotential = -1;
                            //// (6) 最终电位的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            //int FinallyPotentialVSType = (int)VSType.VSOCType;
                            //// (7) 阶跃高度
                            //float StepHeight = 10;
                            //// (8) 阶跃时间
                            //float StepTime = 1f;
                            //// (9) 这里电压量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            //int isVoltageRandAuto = 1;
                            //string VoltageRand = "1000";
                            //// (10) 这里电流量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            //int isCurrentRandAuto = 1;
                            //string currentRand = "1000";

                            strErrorMessage = ElecMachines.Start_Staircase_Cyclic_Voltammetry_Single(
                                (float)ConfigData.InitialPotential,
                                ConfigData.InitialPotentialVSType.Value,
                                (float)ConfigData.TopPotential1,
                                ConfigData.TopPotential1VSType.Value,
                                (float)ConfigData.FinallyPotential,
                                ConfigData.FinallyPotentialVSType.Value,
                                (float)ConfigData.StepHeight,
                                (float)ConfigData.StepTime,
                                ConfigData.IsVoltageRandAuto.Value,
                                $"{(int)ConfigData.VoltageRand}",
                                ConfigData.IsCurrentRandAuto.Value,
                                $"{(int)ConfigData.CurrentRand}"
                            );
                        }
                        break;
                    case ExperimentType.Staircase_Cyclic_Voltammetry_MultipleCycles:
                        {
                            //// (1) 开始电位
                            //float InitialPotential = -0.2f;
                            //// (2) 开始电位的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            //int InitialPotentialVSType = (int)VSType.VSRefType;
                            //// (3) 顶点电位1
                            //float TopPotential1 = 0.8f;
                            //// (4) 顶点电位1的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            //int TopPotential1VSType = (int)VSType.VSRefType;
                            //// (5) 顶点电位2
                            //float TopPotential2 = -0.2f;
                            //// (6) 顶点电位2的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            //int TopPotential2VSType = (int)VSType.VSRefType;
                            //// (7) 最终电位
                            //float FinallyPotential = -0.2f;
                            //// (8) 最终电位的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            //int FinallyPotentialVSType = (int)VSType.VSRefType;
                            ////( 9) 阶跃高度
                            //float StepHeight = 10;
                            //// (10) 阶跃时间
                            //float StepTime = 1f;
                            //// (11) 扫描速率
                            //int CycleCount = 2;
                            //// (12) 这里电压量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            //int isVoltageRandAuto = 1;
                            //string VoltageRand = "1000";
                            //// (13) 这里电流量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            //int isCurrentRandAuto = 1;
                            //string currentRand = "1000";

                            strErrorMessage = ElecMachines.Start_Staircase_Cyclic_Voltammetry_MultipleCycles(
                                (float)ConfigData.InitialPotential,
                                ConfigData.InitialPotentialVSType.Value,
                                (float)ConfigData.TopPotential1,
                                ConfigData.TopPotential1VSType.Value,
                                (float)ConfigData.TopPotential2,
                                ConfigData.TopPotential2VSType.Value,
                                (float)ConfigData.FinallyPotential,
                                ConfigData.FinallyPotentialVSType.Value,
                                (float)ConfigData.StepHeight,
                                (float)ConfigData.StepTime,
                                (int)ConfigData.CycleCount,
                                ConfigData.IsVoltageRandAuto.Value,
                                $"{(int)ConfigData.VoltageRand}",
                                ConfigData.IsCurrentRandAuto.Value,
                                $"{(int)ConfigData.CurrentRand}"
                            );
                        }
                        break;
                    case ExperimentType.Chronoamperonetry:
                        {
                            //// (1) 每点时间
                            //float timePerPoint = 0.1f;
                            //// (2) 持续时间
                            //float continueTime = 10;
                            //// (3) 阶跃电位
                            //float Potential = 0.1f;
                            //// (4) 阶跃电位的参考类型，这里为Ref,也可以设置为VSType.VSOCType
                            //int PotentialVSType = (int)VSType.VSOCType;
                            //// (5) 这里电压量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            //int isVoltageRandAuto = 1;
                            //string VoltageRand = "1000";
                            //// (6) 这里电流量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            //int isCurrentRandAuto = 1;
                            //string currentRand = "1000";

                            strErrorMessage = ElecMachines.Start_ChronoamperonetryParam(
                                (float)ConfigData.TimePerPoint,
                                (float)ConfigData.ContinueTime,
                                (float)ConfigData.InitialPotential,
                                ConfigData.InitialPotentialVSType.Value,
                                ConfigData.IsVoltageRandAuto.Value,
                                $"{(int)ConfigData.VoltageRand}",
                                ConfigData.IsCurrentRandAuto.Value,
                                $"{(int)ConfigData.CurrentRand}"
                            );
                        }
                        break;
                    case ExperimentType.Chronopotentiometry:
                        {
                            //// (1) 每点时间
                            //float timePerPoint = 0.1f;
                            //// (2) 持续时间
                            //float continueTime = 10;
                            //// (3) 电流
                            //float current = 0.1f;
                            //// (4) 这里电压没有自动量程
                            //string VoltageRand = "10000";
                            //// (5) 这里电流量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            //int isCurrentRandAuto = 1;
                            //string currentRand = "1000";

                            strErrorMessage = ElecMachines.Start_ChronopotentiometryParam(
                                (float)ConfigData.TimePerPoint,
                                (float)ConfigData.ContinueTime,
                                (float)ConfigData.Current,
                                $"{(int)ConfigData.VoltageRand}",
                                ConfigData.IsCurrentRandAuto.Value,
                                $"{(int)ConfigData.CurrentRand}"
                            );
                        }
                        break;
                    case ExperimentType.Potentiostatic_Eis:
                        {
                            //// 开始频率
                            //double StartFreq = 10000;
                            //// 结束频率
                            //double EndFreq = 0.1;
                            //// 振幅
                            //float Amplitude = 0.01f;
                            //// 间隔类型，这里设置为对数，也可以设置为线性，如下面注释
                            ////int intervalType =(int)IntervalType.Line
                            //int intervalType = (int)IntervalType.Log;
                            //// 点数
                            //int PointCount = 10;
                            //// 电位
                            //float Voltage = 0;
                            //// 电位相对类型
                            //int VoltageVSType = (int)VSType.VSOCType;
                            //// 这里量程的设置同上面的线性扫描
                            //int isVoltageRandAuto = 1;
                            //string VoltageRand = "1000";

                            //int isCurrentRandAuto = 1;
                            //string currentRand = "1000";

                            strErrorMessage = ElecMachines.Start_EIS(true,
                                (double)ConfigData.StartFreq,
                                (double)ConfigData.EndFreq,
                                (float)ConfigData.Amplitude,
                                ConfigData.IntervalType.Value,
                                ConfigData.PointCount.Value,
                                (float)ConfigData.Voltage,
                                ConfigData.VoltageVSType.Value,
                                ConfigData.IsVoltageRandAuto.Value,
                                $"{(int)ConfigData.VoltageRand}",
                                ConfigData.IsCurrentRandAuto.Value,
                                $"{(int)ConfigData.CurrentRand}"
                            );
                        }
                        break;
                    case ExperimentType.Galvanostatic_Eis:
                        {
                            //// 开始频率
                            //double StartFreq = 10000;
                            //// 结束频率
                            //double EndFreq = 0.1;
                            //// 振幅----------这里如果是7002B,可以把默认值设置为100
                            //float Amplitude = 1;
                            //// 间隔类型，这里设置为对数，也可以设置为线性，如下面注释
                            ////int intervalType =(int)IntervalType.Line
                            //int intervalType = (int)IntervalType.Log;
                            //// 点数
                            //int PointCount = 10;
                            //// 直接输出
                            //float DirectCurrent = 0;
                            //// 直接输出相对类型
                            //int DirectCurrentVSType = (int)VSType.VSRefType;
                            //// 这里量程的设置同上面的线性扫描
                            //int isVoltageRandAuto = 1;
                            //string VoltageRand = "1000";

                            //int isCurrentRandAuto = 1;
                            //string currentRand = "1000";

                            strErrorMessage = ElecMachines.Start_EIS(false,
                                (double)ConfigData.StartFreq,
                                (double)ConfigData.EndFreq,
                                (float)ConfigData.Amplitude,
                                ConfigData.IntervalType.Value,
                                ConfigData.PointCount.Value,
                                (float)ConfigData.InitialPotential,
                                ConfigData.InitialPotentialVSType.Value,
                                ConfigData.IsVoltageRandAuto.Value,
                                $"{(int)ConfigData.VoltageRand}",
                                ConfigData.IsCurrentRandAuto.Value,
                                $"{(int)ConfigData.CurrentRand}"
                            );
                        }
                        break;
                    case ExperimentType.Tafel:
                        {
                            //// (1) 开始电位
                            //float InitialPotential = -0.25f;
                            //// (2) 开始电位的参考类型，这里为VSOCType,也可以设置为VSTypeRef.
                            //int InitialPotentialVSType = (int)VSType.VSOCType;
                            //// (3) 最终电位
                            //float FinallyPotential = 0.25f;
                            //// (4) 最终电位的参考类型，这里为VSOCType,也可以设置为VSType.VSTypeRef
                            //int FinallyPotentialVSType = (int)VSType.VSOCType;
                            //// (5) 阶跃高度
                            //float StepHeight = 0.5f;
                            //// (6) 阶跃时间
                            //float StepTime = 3;
                            //// (7) 这里电压量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            //int isVoltageRandAuto = 1;
                            //string VoltageRand = "1000";
                            //// (8) 这里电流量程设置为自动了,也可以指定为非自动,自动是1,非自动是0,下同
                            //int isCurrentRandAuto = 1;
                            //string currentRand = "1000";

                            strErrorMessage = ElecMachines.Start_Tafel(
                                (float)ConfigData.InitialPotential,
                                ConfigData.InitialPotentialVSType.Value,
                                (float)ConfigData.FinallyPotential,
                                ConfigData.FinallyPotentialVSType.Value,
                                (float)ConfigData.StepHeight,
                                (float)ConfigData.StepTime,
                                ConfigData.IsVoltageRandAuto.Value,
                                $"{(int)ConfigData.VoltageRand}",
                                ConfigData.IsCurrentRandAuto.Value,
                                $"{(int)ConfigData.CurrentRand}"
                            );
                        }
                        break;
                }

                // 查看是否有错误信息，如果没有就是成功
                if (string.IsNullOrEmpty(strErrorMessage))
                {
                    if (ElecMachines.IsExperimenting())
                    {
                        // 这里成功了
                        return (true, "实验初始化成功, 正在执行");
                    }
                }

                // 这里失败了，可以查看失败原因
                return (false, strErrorMessage);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return (false, ex.Message);
            }
        }

        #endregion

        #region 结束实验

        /// <summary>
        /// 结束实验
        /// </summary>
        public (bool, string) StopExp()
        {
            try
            {
                ElecMachines.StopExperiment();
                return (true, "实验结束完成");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return (false, ex.Message);
            }
        }

        #endregion

        #region 检查实验是否正在运行

        /// <summary>
        /// 检查实验是否正在运行
        /// </summary>
        /// <returns>true: 正在运行 false: 停止运行</returns>
        public bool IsExperimenting()
        {
            try
            {
                return ElecMachines.IsExperimenting();
            }
            catch
            {
                return false;
            }
        }

        #endregion

        #region 单步运行实验流程

        /// <summary>
        /// 单步运行实验流程
        /// (运行实验时每秒调用1次)
        /// </summary>
        public List<RunRecordData> SingleStepRunExp()
        {
            if (!IsExperimenting()) { StopExp(); }

            List<RunRecordData> RunRecordDataList = new List<RunRecordData>();

            int resultDataType = ElecMachines.GetResultDataType();
            float[] data = ElecMachines.GetData(resultDataType);
            if (data.Length == 0) { return RunRecordDataList; }

            const int Length = 30;

            if (resultDataType == Voltammetry)
            {
                float[] timeData = ElecMachines.SplitData(data, 3, 0);
                float[] eData = ElecMachines.SplitData(data, 3, 1);
                float[] iData = ElecMachines.SplitData(data, 3, 2);

                int dataCount = timeData.Length;

                if (LogData.Count == 0 || LastResultDataType != resultDataType)
                {
                    LogData.Add("Time(s)".AlignmentStrFunc(Length) + "E(mV)".AlignmentStrFunc(Length) + "I(mA)".AlignmentStrFunc(Length, true));
                }

                // 这里可以使用Time,E,I
                // 例如显示E,I数据
                // 这里把数据打印到了控制台，可以在输出里面看到
                for (int i = 0; i < dataCount; i++)
                {
                    string str = timeData[i].AlignmentStrFunc(Length) + eData[i].AlignmentStrFunc(Length) + iData[i].AlignmentStrFunc(Length, true);
                    System.Diagnostics.Trace.WriteLine(str);
                    LogData.Add(str);
                    RunRecordDataList.Add(new RunRecordData()
                    {
                        Time = timeData[i].AlignmentStrFunc(Length),
                        E = eData[i].AlignmentStrFunc(Length),
                        I = iData[i].AlignmentStrFunc(Length, true)
                    });
                }
            }
            else if (resultDataType == Impedance)
            {
                float[] timeData = ElecMachines.SplitData(data, 6, 0);
                float[] zreData = ElecMachines.SplitData(data, 6, 1);
                float[] zimData = ElecMachines.SplitData(data, 6, 2);
                float[] zData = ElecMachines.SplitData(data, 6, 3);
                float[] freqData = ElecMachines.SplitData(data, 6, 4);
                float[] phaseData = ElecMachines.SplitData(data, 6, 5);

                int dataCount = timeData.Length;

                if (LogData.Count == 0 || LastResultDataType != resultDataType)
                {
                    LogData.Add("Time(s)".AlignmentStrFunc(Length) + "Zre(Ω)".AlignmentStrFunc(Length) + "Zim(Ω)".AlignmentStrFunc(Length) + "Z(Ω)".AlignmentStrFunc(Length) + "Freq(Hz)".AlignmentStrFunc(Length) + "Phase(°)");
                }

                // 这里是交流阻抗实验，可以使用Time,Zre,Zim,Z,Freq,Phase
                // 这里把数据打印到了控制台，可以在输出里面看到
                for (int i = 0; i < dataCount; i++)
                {
                    string str = timeData[i].AlignmentStrFunc(Length) + zreData[i].AlignmentStrFunc(Length) + zimData[i].AlignmentStrFunc(Length) + zData[i].AlignmentStrFunc(Length) + freqData[i].AlignmentStrFunc(Length) + phaseData[i].AlignmentStrFunc(Length, true);
                    System.Diagnostics.Trace.WriteLine(str);
                    LogData.Add(str);
                    RunRecordDataList.Add(new RunRecordData()
                    {
                        Time = timeData[i].AlignmentStrFunc(Length),
                        Zre = zreData[i].AlignmentStrFunc(Length),
                        Zim = zimData[i].AlignmentStrFunc(Length),
                        Z = zData[i].AlignmentStrFunc(Length),
                        Freq = freqData[i].AlignmentStrFunc(Length),
                        Phase = phaseData[i].AlignmentStrFunc(Length, true),
                    });
                }
            }
            else if (resultDataType == Energ)
            {
                float[] timeData = ElecMachines.SplitData(data, 7, 0);
                float[] voltageData = ElecMachines.SplitData(data, 7, 1);
                float[] currentData = ElecMachines.SplitData(data, 7, 2);
                float[] qData = ElecMachines.SplitData(data, 7, 3);
                float[] capacityData = ElecMachines.SplitData(data, 7, 4);
                float[] energyData = ElecMachines.SplitData(data, 7, 5);
                float[] pData = ElecMachines.SplitData(data, 7, 6);

                int dataCount = timeData.Length;

                if (LogData.Count == 0 || LastResultDataType != resultDataType)
                {
                    LogData.Add("Time(s)".AlignmentStrFunc(Length) + "E(mV)".AlignmentStrFunc(Length) + "I(mA)".AlignmentStrFunc(Length) + "Q(mC)".AlignmentStrFunc(Length) + "Capacity(mAh)".AlignmentStrFunc(Length) + "Energy(Wh)".AlignmentStrFunc(Length) + "P(W)");
                }

                // 这里是交流阻抗实验，可以使用Time,Zre,Zim,Z,Freq,Phase
                // 这里把数据打印到了控制台，可以在输出里面看到
                for (int i = 0; i < dataCount; i++)
                {
                    string str = timeData[i].AlignmentStrFunc(Length) + voltageData[i].AlignmentStrFunc(Length) + currentData[i].AlignmentStrFunc(Length) + qData[i].AlignmentStrFunc(Length) + capacityData[i].AlignmentStrFunc(Length) + energyData[i].AlignmentStrFunc(Length) + pData[i].AlignmentStrFunc(Length, true);
                    System.Diagnostics.Trace.WriteLine(str);
                    LogData.Add(str);
                    RunRecordDataList.Add(new RunRecordData()
                    {
                        Time = timeData[i].AlignmentStrFunc(Length),
                        Voltage = voltageData[i].AlignmentStrFunc(Length),
                        Current = currentData[i].AlignmentStrFunc(Length),
                        Q = qData[i].AlignmentStrFunc(Length),
                        Capacity = capacityData[i].AlignmentStrFunc(Length),
                        Energy = energyData[i].AlignmentStrFunc(Length),
                        P = pData[i].AlignmentStrFunc(Length, true),
                    });
                }
            }
            else if (resultDataType == StaticEIS)
            {
                float[] lstTime = ElecMachines.SplitData(data, 14, 0);
                float[] ZRE = ElecMachines.SplitData(data, 14, 1);
                float[] ZIM = ElecMachines.SplitData(data, 14, 2);
                float[] ZValue = ElecMachines.SplitData(data, 14, 3);
                float[] Freq = ElecMachines.SplitData(data, 14, 4);
                float[] JiaoDu = ElecMachines.SplitData(data, 14, 5);
                float[] EDC = ElecMachines.SplitData(data, 14, 6);
                float[] CS = ElecMachines.SplitData(data, 14, 7);
                float[] CS2Reverse = ElecMachines.SplitData(data, 14, 8);
                float[] W = ElecMachines.SplitData(data, 14, 9);
                float[] RP = ElecMachines.SplitData(data, 14, 10);
                float[] CP = ElecMachines.SplitData(data, 14, 11);
                float[] CP2Reverse = ElecMachines.SplitData(data, 14, 12);
                float[] I = ElecMachines.SplitData(data, 14, 13);

                int dataCount = lstTime.Length;

                if (LogData.Count == 0 || LastResultDataType != resultDataType)
                {
                    LogData.Add("Time(s)".AlignmentStrFunc(Length) + "Zre(Ω)".AlignmentStrFunc(Length) + "Zim(Ω)".AlignmentStrFunc(Length) + "Z(Ω)".AlignmentStrFunc(Length) + "Freq(Hz)".AlignmentStrFunc(Length) + "Phase(°)".AlignmentStrFunc(Length) + "EDC(mV)");
                }

                // 这里可以使用Time,E,I
                // 例如显示E,I数据
                // 这里把数据打印到了控制台，可以在输出里面看到
                for (int i = 0; i < dataCount; i++)
                {
                    string str = lstTime[i].AlignmentStrFunc(Length) + ZRE[i].AlignmentStrFunc(Length) + ZIM[i].AlignmentStrFunc(Length) + ZValue[i].AlignmentStrFunc(Length) + Freq[i].AlignmentStrFunc(Length) + JiaoDu[i].AlignmentStrFunc(Length) + EDC[i].AlignmentStrFunc(Length, true);
                    System.Diagnostics.Trace.WriteLine(str);
                    LogData.Add(str);
                    RunRecordDataList.Add(new RunRecordData()
                    {
                        LstTime = lstTime[i].AlignmentStrFunc(Length),
                        Zre = ZRE[i].AlignmentStrFunc(Length),
                        Zim = ZIM[i].AlignmentStrFunc(Length),
                        ZValue = ZValue[i].AlignmentStrFunc(Length),
                        Freq = Freq[i].AlignmentStrFunc(Length),
                        JiaoDu = JiaoDu[i].AlignmentStrFunc(Length),
                        EDC = EDC[i].AlignmentStrFunc(Length, true),
                    });
                }
            }

            // 如果日志记录数量大于一万条时则清空数据
            if (LogData.Count > 10000) { LogData.Clear(); }

            // 记录本次返回值
            LastResultDataType = resultDataType;

            if (ElecMachines.IsOverLoad())
            {
                System.Diagnostics.Trace.WriteLine("过载了");
            }

            return RunRecordDataList;
        }

        #endregion

    }
}
